package com.stopboot.code.generator.utils;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

/**
 * @description:
 * @author: Lianyutao
 * @create: 2019-10-16 17:57
 * @version:
 **/
public class FreemarkerUtil {

    /**
     * freemarker config
     */
    private static Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

    /**
     *
     *
     */
    static {
        configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/templates");
        configuration.setNumberFormat("#");
        configuration.setClassicCompatible(true);
        configuration.setDefaultEncoding("UTF-8");
        configuration.setLocale(Locale.CHINA);
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }

    /**
     * 根据模版内容生成内容
     * @param templateContent 模版内容
     * @param data 数据
     * @return
     * @throws IOException
     * @throws TemplateException
     */
    public static StringWriter processByTemplateContent(String templateContent, Object data)
            throws IOException, TemplateException {
        StringTemplateLoader stringLoader = new StringTemplateLoader();
        String templateName = "templateName-"+System.currentTimeMillis();
        // 往模板输入内容
        stringLoader.putTemplate(templateName, templateContent);
        // 设置模板
        configuration.setTemplateLoader(stringLoader);
        Template template = configuration.getTemplate(templateName);
        StringWriter result = new StringWriter();
        template.process(data, result);
        return result;
    }

    /**
     * 根据模版文件生成
     * @param templatePath
     * @param data
     * @return
     * @throws IOException
     * @throws TemplateException
     */
    public static StringWriter process(String templatePath, Object data)
            throws IOException, TemplateException {
        configuration.setTemplateLoader(null);
        configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/templates");
        Template template = configuration.getTemplate(templatePath);
        StringWriter result = new StringWriter();
        template.process(data, result);
        return result;
    }

    public static StringWriter process(String templateName, Object params, String language)
            throws IOException, TemplateException {
        configuration.setTemplateLoader(null);
        configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/templates");
        Template template = configuration.getTemplate(templateName);
        StringWriter result = new StringWriter();
        template.process(params, result);
        //如果是java文件就格式化一下
//        if (language.equals("java")) {
//            String javaFormat = CodeFormatterUtil.javaFormat(result.toString());
//            StringWriter newResult = new StringWriter();
//            newResult.write(javaFormat);
//            return newResult;
//        }
        return result;
    }


    public static void test(String ftlName, Object data) throws IOException, TemplateException {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("/Users/91haoke/Dev/lyt/stopboot/src/main/resources/templates"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate(ftlName);
        //5.创建数据模型
        //6.创建Writer对象
        StringWriter result = new StringWriter();
        //7.输出
        template.process(data, result);

        System.out.println(result);
    }
    public static void testSave(String ftlName, String savePath, String fileName, Object data) throws IOException, TemplateException {
        testSave(ftlName, savePath, fileName, data, "java");
    }

    public static void testSave(String ftlName, String savePath, String fileName, Object data, String language) throws IOException, TemplateException {
        //1.创建配置类
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        //2.设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("/Users/lyt/Dev/cai/stopboot-new/stopboot/src/main/resources/"));
        //3.设置字符集
        configuration.setDefaultEncoding("utf-8");
        //4.加载模板
        Template template = configuration.getTemplate(ftlName);
        //5.创建数据模型
        //6.创建Writer对象
        StringWriter result = new StringWriter();
        //7.输出
        template.process(data, result);

        // 8.将结果保存到文件中
//        if (language.equals(LanguageTypeEnum.JAVA)) {
//            String javaFormat = CodeFormatterUtil.javaFormat(result.toString());
//            StringWriter newResult = new StringWriter();
//            newResult.write(javaFormat);
//
//            System.out.println(result);
//        }

        //
        FileUtils.write(new File(savePath + fileName + ".java"), result.getBuffer(), StandardCharsets.UTF_8);

    }


    public static void main(String[] args) {


    }


}
